package com.privatetime.common.tag;

import javax.servlet.jsp.JspException;

import com.privatetime.common.util.ResponseUtil;

public class PaginationTag extends AbstractBodyTagSupport {

	private static final long serialVersionUID = 1L;

	private static final String CURRENT_CLASS = " class=\"current\"";

	private int currentIndex = 1;// current index

	private int totalNum = 0;// the number of all data

	private int totalPages = 0;// total pages

	private int pageSize = 10;// every page size

	public PaginationTag() {

	}

	private void isFirst(StringBuffer sb) {
		if (currentIndex <= 1) {
			sb.append("\t").append("<li>First</li>").append(NEW_LINE);
		} else {
			sb.append("\t").append("<li><a href=\"?page=1\">First</a></li>")
					.append(NEW_LINE);
		}
	}

	private void isLast(StringBuffer sb) {
		if (currentIndex >= totalPages) {
			sb.append("\t").append("<li>End</li>").append(NEW_LINE);
		} else {
			sb.append("\t").append(
					"<li><a href=\"?page=" + totalPages + "\">End</a></li>")
					.append(NEW_LINE);
		}
	}

	private void hasNext(StringBuffer sb) {
		if (currentIndex < totalPages) {
			sb.append("\t").append(
					"<li><a href=\"?page=" + (currentIndex + 1)
							+ "\">Next</a></li>").append(NEW_LINE);
		} else {
			sb.append("\t").append("<li>Next</li>").append(NEW_LINE);
		}
	}

	private void hasPrevious(StringBuffer sb) {
		if (currentIndex > 1) {
			sb.append("\t").append(
					"<li><a href=\"?page=" + (currentIndex - 1)
							+ "\">Previous</a></li>").append(NEW_LINE);
		} else {
			sb.append("\t").append("<li>Previous</li>").append(NEW_LINE);
		}
	}

	private void calculateTotalPages() {
		if (totalNum <= pageSize) {
			totalPages = 1;
		} else {
			totalPages = (totalNum % pageSize) == 0 ? (totalNum / pageSize)
					: (totalNum / pageSize) + 1;
		}
	}

	@Override
	protected void end() throws JspException {
		calculateTotalPages();
		if (currentIndex < 1)
			currentIndex = 1;
		else if (currentIndex > totalPages)
			currentIndex = totalPages;

		StringBuffer sb = new StringBuffer();
		sb.append("<!-- Pagination Start -->").append(NEW_LINE);
		sb.append("<div id=\"pagination\">").append(NEW_LINE);
		sb.append("<ul>").append(NEW_LINE);

		isFirst(sb);
		hasPrevious(sb);

		// page number begin
		int startIndex = (currentIndex + 5 > totalPages ? totalPages - 9
				: currentIndex - 4);
		int endIndex = (currentIndex < 5 ? 10 : (currentIndex + 5));
		startIndex = (startIndex < 1 ? 1 : startIndex);
		endIndex = (totalPages < endIndex ? totalPages : endIndex);
		for (int j = startIndex; (j <= endIndex); j++) {
			sb.append("\t");
			if (currentIndex == j) {
				sb.append("<li").append(CURRENT_CLASS).append(">").append(j)
						.append("</li>");
			} else {
				sb.append("<li>").append("<a href=\"?page=").append(j).append(
						"\">").append(j).append("</a>").append("</li>");
			}
			sb.append(NEW_LINE);
		}
		// page number end

		hasNext(sb);
		isLast(sb);
		sb.append("</ul>").append(NEW_LINE);
		sb.append("</div>").append(NEW_LINE);
		sb.append("<!-- Pagination End -->").append(NEW_LINE);
		logger.info("currentIndex=" + currentIndex + " totalNum=" + totalNum
				+ " pageSize=" + pageSize + " totalPages=" + totalPages);
		ResponseUtil.Writer(pageContext, sb.toString());
	}

	@Override
	protected void start() {
		
	}

	public int getCurrentIndex() {
		return currentIndex;
	}

	public void setCurrentIndex(int currentIndex) {
		this.currentIndex = currentIndex;
	}

	public int getTotalNum() {
		return totalNum;
	}

	public void setTotalNum(int totalNum) {
		this.totalNum = totalNum;
	}

	public int getTotalPages() {
		return totalPages;
	}

	public void setTotalPages(int totalPages) {
		this.totalPages = totalPages;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

}
